### 随机生成角度
import random,math
import numpy as np

def calculateRandomDegree(x1,x2,y1,y2,z1,z2):
    # 计算两个点的水平方向
    dx = x2 - x1
    dy = y2 - y1
    azimuth = math.atan2(dy, dx)  # atan2返回值的范围是-π到π
    azimuth_degrees_p = math.degrees(azimuth)  # 将弧度转换为度

    dx = x1-x2
    dy = y1-y2
    azimuth = math.atan2(dy, dx)  # atan2返回值的范围是-π到π
    azimuth_degrees_q = math.degrees(azimuth)  # 将弧度转换为度

    # 计算两个点的俯仰角
    dz = z2 - z1
    distance = math.sqrt(dx**2 + dy**2)
    elevation = math.atan2(dz, distance)  # 俯仰角
    elevation_degrees_p = math.degrees(elevation)  # 将弧度转换为度

    dz = z1 - z2
    distance = math.sqrt(dx**2 + dy**2)
    elevation = math.atan2(dz, distance)  # 俯仰角
    evalution_degrees_q = math.degrees(elevation)  # 将弧度转换为度

    return azimuth_degrees_p,elevation_degrees_p

def calculateAngleFromVector(v1, v2):
    # 计算两个向量的点乘
    dot_product = np.dot(v1, v2)
    # 计算两个向量的模
    norm_v1 = np.linalg.norm(v1)
    norm_v2 = np.linalg.norm(v2)
    if(norm_v1 == 0 or norm_v2 == 0):
        return 0.0
    # 计算夹角的余弦值
    cos_theta = dot_product / (norm_v1 * norm_v2)
    # 计算夹角，确保余弦值在-1到1之间
    angle = np.arccos(np.clip(cos_theta, -1.0, 1.0))
    # 将弧度转换为度
    angle_degrees = np.degrees(angle)
    return angle_degrees

def getDegreeFromVectorAndCartesianCoordinates(x,y,z,vector):
    '''
    x,y,z: 三个坐标轴的单位向量
    reference: https://blog.csdn.net/liushao1031177/article/details/119776495
    '''
    # 给定向量 v
    v = np.array(vector)
    x = np.array(x)
    y = np.array(y)
    z = np.array(z)
    # 计算 v 在 z 方向上的分量
    v_dot_z = np.dot(v, z)
    v_z = v_dot_z * z
    # 计算 v 在 xy 平面上的分量
    v_xy = v - v_z

    H_degree = calculateAngleFromVector(v_xy, x)
    if(H_degree==0):
        return 0.0,90.0
    V_degree = calculateAngleFromVector(v, v_xy)
    if(V_degree==0):
            return 90.0,0.0
    return H_degree,V_degree

    

# def randomAngle():
#     # 生成第一个点的坐标
#     x1 = random.uniform(-10, 10)  # x坐标在-10到10之间
#     y1 = random.uniform(-10, 10)  # y坐标在-10到10之间
#     z1 = random.uniform(-10, 10)  # z坐标在-10到10之间

#     # 生成第二个点的坐标
#     x2 = random.uniform(-10, 10)  # x坐标在-10到10之间
#     y2 = random.uniform(-10, 10)  # y坐标在-10到10之间
#     z2 = random.uniform(-10, 10)  # z坐标在-10到10之间
#     return calculateRandomDegree(x1,x2,y1,y2,z1,z2)

### 随机生成点簇
def randomCluster(x1,x2,y1,y2,z1,z2):
    cluster = []
    for i in range(2):
        cluster.append([])
    for i in range(0,2):
        for j in range(0,3):
            x = random.uniform(-10, 10)  # x坐标在-10到10之间
            y = random.uniform(-10, 10)  # y坐标在-10到10之间
            z = random.uniform(-10, 10)  # z坐标在-10到10之间
            cluster[i].append([x,y,z])
    return cluster

# 定义反函数 原函数为y = 10log(x)
def inverse_function(y):
    return 10 ** (y/10)

def calculateDegreeFromTwoPoint(x1,x2,y1,y2,z1,z2):
    # 计算两个点的水平方向
    dx = x2 - x1
    dy = y2 - y1
    azimuth = math.atan2(dy, dx)  # atan2返回值的范围是-π到π
    azimuth_degrees_p = math.degrees(azimuth)  # 将弧度转换为度

    dx = x1-x2
    dy = y1-y2
    azimuth = math.atan2(dy, dx)  # atan2返回值的范围是-π到π
    azimuth_degrees_q = math.degrees(azimuth)  # 将弧度转换为度

    # 计算两个点的俯仰角
    dz = z2 - z1
    distance = math.sqrt(dx**2 + dy**2)
    elevation = math.atan2(dz, distance)  # 俯仰角
    elevation_degrees_p = math.degrees(elevation)  # 将弧度转换为度

    dz = z1 - z2
    distance = math.sqrt(dx**2 + dy**2)
    elevation = math.atan2(dz, distance)  # 俯仰角
    evalution_degrees_q = math.degrees(elevation)  # 将弧度转换为度

    return azimuth_degrees_p,azimuth_degrees_q,elevation_degrees_p,evalution_degrees_q